<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>
  <head>
    <title>Test Datastore</title>
  </head>

  <body>

<%
    //String guestbookName = request.getParameter("guestbookName");
    //if (guestbookName == null) {
    //    guestbookName = "default";
    //}
    //pageContext.setAttribute("guestbookName", guestbookName);
    //UserService userService = UserServiceFactory.getUserService();
    //User user = userService.getCurrentUser();
    //if (user != null) {
      //pageContext.setAttribute("user", user);
%>
<!--<p>Hello, ${fn:escapeXml(user.nickname)}! (You can
<a href="<%//= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>-->
<%
    //} else {
%>
<p><!--Hello!
<a href="<%//= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to include your name with greetings you post.</p>-->
<%
    //}
%>

<%
	String keyName = "blahblah123";
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key testKey = KeyFactory.createKey("TestStuff", keyName);
    // Run an ancestor query to ensure we see the most up-to-date
    // view of the Greetings belonging to the selected Guestbook.
    Query query = new Query("Entries", testKey);
    List<Entity> entries = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
    if (entries.isEmpty()) {
        %>
        <p>Entry '${fn:escapeXml(keyName)}' is empty.</p>
        <%
    } else {
        %>
        <p>Entries in '${fn:escapeXml(keyName)}'.</p>
        <%
        for (Entity entry : entries) {
            pageContext.setAttribute("entry_content", entry.getProperty("content"));
            %>
            '${fn:escapeXml(greeting_content)}'
            <%
        }
    }
%>

    <form action="/test_database" method="post">
      <div><textarea name="content" rows="3" cols="60"></textarea></div>
      <div><input type="submit" value="submit" /></div>
      <input type="hidden" name="keyName" value="${fn:escapeXml(keyName)}"/>
    </form>

  </body>
</html>